import { app, BrowserWindow, ipcMain, desktopCapturer, session } from 'electron';
// import WebSocket from 'ws';
import * as path from 'path';



var confing: any = {
  sources:null
}

let mainWindow: BrowserWindow | null = null;

function createWindow() {
  // 创建窗口
  mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    fullscreen: true,  // 设置窗口为全屏模式
    webPreferences: {
      preload: path.join(__dirname, 'preload/index.js'),
      nodeIntegration: false,
      contextIsolation: true,
    },
  });
  if (process.env.NODE_ENV === 'development') {
    mainWindow.loadURL('http://localhost:3000');
  } else {
    mainWindow.loadFile(path.join(__dirname, 'dist', 'index.html'));
  }

  mainWindow.on('closed', () => {
    mainWindow = null;
  });
  setTimeout(() => {
    mainWindow.webContents.openDevTools();
  }, 1500);
}

app.whenReady().then(() => {
  createWindow();
  session.defaultSession.setDisplayMediaRequestHandler((request, callback) => {
    desktopCapturer.getSources({ types: ['screen', 'window'] }).then((sources) => {
      confing.sources = sources
      callback({ video: sources[0], audio: 'loopback' })
    })
  }, { useSystemPicker: true })
  app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) {
      const WinMain = createWindow();

    }
  });
});

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

ipcMain.handle('get-node-data', async () => {
  return 'Hello from main process (Node.js)';
});
// 捕获屏幕或窗口的内容
ipcMain.handle('get-screen', async () => {
  const sources = await desktopCapturer.getSources({ types: ['screen', 'window'] });
  // 你可以根据需要选择某个特定的屏幕或窗口
  const selectedSource = sources; // 选择第一个屏幕（你也可以根据条件选择）

  return selectedSource;
});





